using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using T2.WebSecurity.ASPMVC.Entity;
using T2.WebSecurity.ASPMVC.Entity.Interface;
using T2.WebSecurity.ASPMVC.Entity.Search.Interface;
using T2.WebSecurity.ASPMVC.Entity.Translators;
using T2.WebSecurity.ASPMVC.Entity.Translators.Search;
using T2.WebSecurity.Business.Entities.Interfaces;
using T2.WebSecurity.Business.Entities.Search.Interfaces;
using T2.WebSecurity.Business.Workflows;

namespace T2.WebSecurity.ASPMVC.ViewModel
{

	public class ApplicationViewModel : BaseViewModel
	{
		Business.BusinessService bs = new Business.BusinessService();
		public ApplicationViewModel(bool loadParent)
		{
		if (loadParent)
			{
			}
		}
		
		private String m_header;
		public String Header
        {
            get
            {
                return "Application";
            }
			
			set
            {
                m_header = value;
            }
        }
		
		private String m_title;
		public String Title
        {
            get
            {
                return "Application";
            }
			
			set
            {
                m_title = value;
            }
        }
		
		private IApplication_ui m_model;
		public IApplication_ui Model
		{
			get
			{
				return m_model;
			}
			set
			{
				m_model = value;
			}
		}

        
		public void LoadModel(IApplication_ui param)
        {
            
            ApplicationUITranslator trans = new ApplicationUITranslator();
            IApplication_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserName", User.UserName);
            wfInput.Add("UserId", User.ProviderUserKey);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationQuery(), wfInput);

            IApplication_biz tempRetVal = (IApplication_biz)wfOutput["RetVal"];

            //List<IApplication_biz> tempRetVal = FilterListByLoginUser(bs.ApplicationController_Get(bizParam, true, true));
            IApplication_ui retVal = new Application_ui();
            if (tempRetVal != null )
            {
                retVal = trans.TranslateFromBusinessLayer(tempRetVal);
            }

            m_model = retVal;

        }

        public Int32 Set(IApplication_ui param)
        {
            ApplicationUITranslator trans = new ApplicationUITranslator();
            IApplication_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserName", User.UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationNonQuery(), wfInput);

            IDictionary<string, int> r = (IDictionary<string, int>)wfOutput["RetVal"];

            //IDictionary<string,int> r = bs.ApplicationController_Set(bizParam);
            Int32 retVal = r.Where(z => z.Key == "Return_Value").Select(p => p.Value).FirstOrDefault(); 
			return retVal;
        }

       

        public List<IApplication_ui> SearchList(IApplicationSearch_ui param)
        {
            ApplicationUISearchTranslator transSearch = new ApplicationUISearchTranslator();
            IApplicationSearch_biz bizParam = transSearch.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();
            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", null);
            wfInput.Add("ParamSearch", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            wfInput.Add("UserName", User.UserName);
            wfInput.Add("UserId", User.ProviderUserKey);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationQuery(), wfInput);

            IApplication_biz[] retValBiz = (IApplication_biz[])wfOutput["RetValCollection"];

            //List<IApplication_biz> retValBiz = FilterListByLoginUser(bs.ApplicationController_Search(bizParam, true, true));
            
            List<IApplication_ui> retVal = new List<IApplication_ui>();
            ApplicationUITranslator trans = new ApplicationUITranslator();
            foreach (IApplication_biz item in retValBiz)
            {
                IApplication_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }

        

        public List<IApplication_ui> ModelList(IApplication_ui param)
        {
            
            ApplicationUITranslator trans = new ApplicationUITranslator();

            IApplication_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string,object> wfInput = new Dictionary<string,object>();
            
            wfInput.Add("BusinessService",bs);
            wfInput.Add("Param",bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames",Roles.GetRolesForUser());
            wfInput.Add("UserName",User.UserName);
            wfInput.Add("UserId", User.ProviderUserKey);

            // activation of workflow
            IDictionary<string,object> wfOutput = WorkflowInvoker.Invoke(new ApplicationQuery(), wfInput);

            IApplication_biz[] retValBiz = (IApplication_biz[])wfOutput["RetValCollection"];

            //List<IApplication_biz> retValBiz = FilterListByLoginUser(bs.ApplicationController_Get(bizParam, true, true));


            List<IApplication_ui> retVal = new List<IApplication_ui>();
            foreach (IApplication_biz item in retValBiz)
            {
                IApplication_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }

		
	}
}
